<template>
  <div>
    <van-nav-bar title="账户信息" left-arrow @click-left="GOback" placeholder fixed :border="false" />
    <!-- 头像信息 -->
    <div class="headportrait">
      <input type="file" class="profileinfopanel-upload" accept="image/png,image/gif,image/jpeg" @change="(e)=>{upload(e)}">
      <h2>头像</h2>
      <div class="headportrait-div">
        <img :src="'//elm.cangdu.org/img/'+UserData.avatar" alt="" class="headportrait-div-top">
        <van-icon name="arrow" size="0.8rem" color="#ddd" />
      </div>
    </div>
    <!-- 用户名 -->
    <li class="headportrait headportraitwo" @click="userModify">
      <h2>用户名</h2>
      <div class="headportrait-div">
        <p>{{UserData.username}}</p>
        <van-icon name="arrow" size="0.8rem" color="#ddd" />
      </div>
    </li>
    <!-- 收货 地址 -->
    <li class="headportrait headportraitwo headportraithree" @click="addressesThe">
      <h2>收货地址</h2>
      <div class="headportrait-div">
        <van-icon name="arrow" size="0.8rem" color="#ddd" />
      </div>
    </li>
    <div class="bind-phone">账号绑定</div>
    <!-- 用户手机号 -->
    <li class="headportrait headportraitwo headportraithree" @click="ModifyPhone">
      <h2>
        <img src="@/assets/img/index.png" alt="" style="display: inline-block; margin-right: 0.4rem;">
        手机
      </h2>
      <div class=" headportrait-div">
        <van-icon name="arrow" size="0.8rem" color="#ddd" />
      </div>
    </li>
    <div class="bind-phone">登录密码</div>
    <!-- 修改登录密码 -->
    <li class="headportrait headportraitwo headportraitth" @click="ChangePass">
      <h2>
        手机
      </h2>
      <div class=" headportrait-div">
        <p>修改</p>
        <van-icon name="arrow" size="0.8rem" color="#ddd" />
      </div>
    </li>
    <div class="exitlogin" @click="Logout">退出登录</div>
    <!-- 修改手机号的弹框 -->
    <van-dialog v-model="show" confirmButtonColor="#3199e8">
      <template #default>
        <div class="tip_icon">
          <span></span>
          <span></span>
        </div>
        <p class="tip_text">请在手机APP中设置</p>
      </template>
    </van-dialog>
    <!-- 退出登录的弹框 -->
    <van-dialog v-model="show1" confirmButtonColor="#3199e8" show-cancel-button @confirm="exit">
      <template #default>
        <div class="tip_icon">
          <span></span>
          <span></span>
        </div>
        <p class="tip_text">是否退出登录</p>
      </template>
    </van-dialog>
  </div>

</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      // 弹框状态
      show: false,
      show1: false
    }
  },
  computed: {
    ...mapState(['UserData'])
  },
  methods: {
    GOback() {
      this.$router.go(-1)
    },
    // 跳转至修改用户名
    userModify() {
      this.$router.push('/user/info/setusername')
    },
    // 跳转收货地址
    addressesThe() {
      this.$router.push('/user/info/address')
    },
    // 点击手机号显示只允许APP里修改手机号
    ModifyPhone() {
      this.show = true
    },
    // 点击修改跳转至修改密码
    ChangePass() {
      this.$router.push('/forget')
    },
    // 点击显示退出登录弹框
    Logout() {
      this.show1 = true
    },
    // 点击弹框的确认退出登录
    async exit() {
      const { data: res } = await this.reques.get('v2/signout')
      console.log(res)
      window.sessionStorage.removeItem('user_id')
      this.$router.replace('/user')
    },
    // 上传图片
    async upload(e) {
      const data = new FormData()
      data.append('file', e.target.files[0])
      console.log(data)
      const { data: res } = await this.reques.post(`eus/v1/users/72032/avatar`, {
        credentials: 'include',
        body: data
      })
      console.log(res)
      console.log(e.target.files[0])
    }
  },
  mounted() {
    this.$store.dispatch('getUser')
  }
}
</script>

<style lang="less" scoped>
.headportrait {
  margin-top: 0.4rem;
  padding: 0.5rem 0.4rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid #ddd;
  background: #fff;
  .profileinfopanel-upload {
    display: block;
    position: absolute;
    opacity: 0;
    top: 2.35rem;
    left: 0;
    width: 100%;
    height: 3.1rem;
  }
  h2 {
    font-size: 0.55rem;
    color: #333;
    font-weight: 500;
    display: flex;
    align-items: center;
  }
  .headportrait-div {
    display: flex;
    align-items: center;
    img {
      width: 2rem;
      height: 2rem;
      border-radius: 50%;
      margin-right: 0.2rem;
    }
    p {
      text-align: left;
      line-height: 1.39rem;
      font-size: 0.6rem;
      color: #999;
      margin-right: 0.2rem;
      font-weight: 100;
    }
  }
}
.headportraitwo {
  margin-top: 0;
  padding: 0.3rem 0.4rem;
}
.headportraithree {
  border-bottom: 1px solid #ddd;
  padding: 0.5rem 0.4rem;
}
.bind-phone {
  padding: 0.4rem;
  color: #666;
}
.headportraitth {
  border-bottom: 1px solid #ddd;
}
.exitlogin {
  width: 96%;
  margin: 1.3rem auto 0;
  line-height: 1.5rem;
  border-radius: 5px;
  text-align: center;
  background: #3199e8;
  font-size: 0.6rem;
  color: #fff;
}
</style>